//
//  HomeViewController.m
//  kularkDemo
//
//  Created by szblsx2 on 2020/3/18.
//  Copyright © 2020 szblsx. All rights reserved.
//

#import "HomeViewController.h"
#import "ChatViewController.h"
#import "KLSdk.h"

@interface HomeViewController ()

@property (weak, nonatomic) IBOutlet UITextField *ipTF;
@property (weak, nonatomic) IBOutlet UITextField *portTF;
@property (weak, nonatomic) IBOutlet UITextField *roomTF;
@property (weak, nonatomic) IBOutlet UITextField *passwdTF;
@property (weak, nonatomic) IBOutlet UITextField *userNameTF;
@property (weak, nonatomic) IBOutlet UISegmentedControl *segment;

@property (weak, nonatomic) IBOutlet UIButton *creatButton;
@property (weak, nonatomic) IBOutlet UIButton *joinButton;


@property (nonatomic, strong) KLMeetUser *myInfo;

@end

@implementation HomeViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"百灵一会";
    self.creatButton.layer.borderColor = UIColor.lightGrayColor.CGColor;
    self.creatButton.layer.borderWidth = 1.0;
    self.joinButton.layer.borderColor = UIColor.lightGrayColor.CGColor;
    self.joinButton.layer.borderWidth = 1.0;
    
    NSDictionary *selectedAttributes = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:NSForegroundColorAttributeName];
    [self.segment setTitleTextAttributes:selectedAttributes forState:UIControlStateSelected];
    
    //初始化
    _userNameTF.text =  [KLUserInfo shared].userName;

    //    _ipTF.text = @"120.238.78.214";
    _ipTF.text = @"192.168.2.234";
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = NO;
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:45/255.f green:45/255.f blue:57/255.f alpha:1.0];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor],
                                                                      NSFontAttributeName : [UIFont fontWithName:@"Helvetica-Bold" size:17]}];
}

- (IBAction)segmentAction:(UISegmentedControl *)sender
{
    if (sender.selectedSegmentIndex == 0) {
        self.myInfo.meetingType = kMeetingVideo;
    }else{
        self.myInfo.meetingType = kMeetingAudio;
    }
}

/**创建会议*/
- (IBAction)creatButtonAction:(UIButton *)sender
{
    
    if (self.roomTF.text.length == 0) {
        [self.roomTF resignFirstResponder];
        [WHToast showMessage:@"请输入房间号"];
        return;
    }
    
    self.myInfo.userName = self.userNameTF.text;

    [[KLSdk shared] initSDKWithServer:self.ipTF.text port:self.portTF.text];
    
    __weak __typeof(self) ws = self;
    [[KLSdk shared] enterMeetingWithRoomId:self.roomTF.text myInfo:self.myInfo completion:^(BOOL result) {
        if (result) {
            ChatViewController *chatVc = [ChatViewController new];
            UINavigationController *naviVc = [[UINavigationController alloc] initWithRootViewController:chatVc];
            chatVc.roomId = ws.roomTF.text;
            chatVc.myInfo = ws.myInfo;
            naviVc.modalPresentationStyle = UIModalPresentationOverFullScreen;
            [ws presentViewController:naviVc animated:YES completion:nil];
        }else{
            [WHToast showMessage:@"创建会议失败" originY:0 duration:2.0];
        }
    }];
    
    
}

#pragma mark - Getter

- (KLMeetUser *)myInfo
{
    if (!_myInfo) {
        _myInfo = [[KLMeetUser alloc] init];
        _myInfo.userKind = kMeetUserLocal;
        _myInfo.meetingType = kMeetingVideo;
        _myInfo.openMicrophone = NO;
        _myInfo.openLoudspeaker = NO;
        _myInfo.userId = [KLUserInfo shared].userId;
        _myInfo.userHead = @"http://att3.citysbs.com/200x200/hangzhou/2020/04/15/11/dd6719bd4287d9efd49434c43563a032_v2_.jpg";
    }
    return _myInfo;
}


@end
